from lxml import etree
import mysql.connector

def parse_pagesource(pagesource):
    result1 = []
    result = {}
    response = etree.HTML(pagesource)
    if len(response.xpath('//td[contains(text(),"产品名称")]/../td[last()]//text()')) > 0:

        result['产品名称'] = response.xpath('//td[contains(text(),"产品名称")]/../td[last()]//text()')[0].strip()
    else:
        result['产品名称'] = ' '
    if len(response.xpath('//td[contains(text(),"总质量(KG)")]/../td[2]//text()')) > 0:
        result['总质量(KG)'] = response.xpath('//td[contains(text(),"总质量(KG)")]/../td[2]//text()')[0].strip()
    else:
        result['总质量(KG)'] = ' '
    if len(response.xpath('//td[contains(text(),"发布日期")]/../td[2]//text()')):
        result['发布日期'] = response.xpath('//td[contains(text(),"发布日期")]/../td[2]//text()')[0].strip()
    else:
        result['发布日期'] = ' '
    if len(response.xpath('//td[contains(text(),"整备质量(KG)")]/../td[2]//text()')) > 0:
        result['整备质量(KG)'] = response.xpath('//td[contains(text(),"整备质量(KG)")]/../td[2]//text()')[0].strip()
    else:
        result['整备质量(KG)'] = ' '
    if len(response.xpath('//td[contains(text(),"公告批次")]/../td[2]//text()')) > 0:
        result['公告批次'] = response.xpath('//td[contains(text(),"公告批次")]/../td[2]//text()')[0].strip()
    else:
        result['公告批次'] = ' '
    if len(response.xpath('//td[contains(text(),"最高车速(KM/H)")]/../td[2]//text()')) > 0:
        result['最高车速(KM/H)'] = response.xpath('//td[contains(text(),"最高车速(KM/H)")]/../td[2]//text()')[0].strip()
    else:
        result['最高车速(KM/H)'] = ' '
    if len(response.xpath('//td[contains(text(),"轴距(mm)")]/../td[4]//text()')) > 0:
        result['轴距(mm)'] = response.xpath('//td[contains(text(),"轴距(mm)")]/../td[4]//text()')[0].strip()
    else:
        result['轴距(mm)'] = ' '
    if len(response.xpath('//td[contains(text(),"生产地址")]/../td[2]//text()')) > 0:
        result['生产地址'] = response.xpath('//td[contains(text(),"生产地址")]/../td[2]//text()')[0].strip()
    else:
        result['生产地址'] = ' '
    if len(response.xpath('//td[contains(text(),"企业名称")]/../td[2]//text()')) > 0:
        result['企业名称'] = response.xpath('//td[contains(text(),"企业名称")]/../td[2]//text()')[0].strip()
    else:
        result['企业名称'] = ' '

    if len(response.xpath('//a[contains(text(),"VIN")]/../../td[2]//text()')) > 0:
        result['VIN'] = response.xpath('//a[contains(text(),"VIN")]/../../td[2]//text()')[0].strip()
    else:
        result['VIN'] = ' '
    if len(response.xpath('//td[contains(text(),"外形(长/宽/高)(mm)")]/../td[2]//text()')) > 0:
        result['外形(长/宽/高)(mm)'] = response.xpath('//td[contains(text(),"外形(长/宽/高)(mm)")]/../td[2]//text()')[0].strip()
    else:
        result['外形(长/宽/高)(mm)'] = ' '

    return result

def createDictCSV(fileName="", data={}):
    keys = ', '.join(['`%s`' % i for i in data.keys()])
    values = ', '.join(['\'%s\'' % i for i in data.values()])
    sql = 'insert into car_zyqc (%s) values (%s)' % (keys, values)





    conn = mysql.connector.connect(user='diagnose_report', password='launch@1234',
                                       host='rm-wz9v9e07mh753tk321o.mysql.rds.aliyuncs.com',
                                       database='car_spider')

    cursor = conn.cursor()

    cursor.execute(sql)


    conn.commit()
    cursor.close()
    conn.close()




